package com.lzdongrui.backend.service.impl;

import java.util.List;

import com.lzdongrui.common.exception.ServiceException;
import com.lzdongrui.common.utils.DateUtils;
import com.lzdongrui.common.utils.StringUtils;
import com.lzdongrui.common.utils.bean.BeanValidators;
import com.lzdongrui.common.utils.snowid.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lzdongrui.backend.mapper.CampusMapper;
import com.lzdongrui.backend.domain.Campus;
import com.lzdongrui.backend.service.ICampusService;

import javax.validation.Validator;

/**
 * 校区Service业务层处理
 * 
 * @author wyh
 * @date 2023-04-28
 */
@Service
public class CampusServiceImpl implements ICampusService
{
    private static final Logger log = LoggerFactory.getLogger(CampusServiceImpl.class);

    @Autowired
    private CampusMapper campusMapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    protected Validator validator;

    /**
     * 查询校区
     * 
     * @param id 校区主键
     * @return 校区
     */
    @Override
    public Campus selectCampusById(Long id)
    {
        return campusMapper.selectCampusById(id);
    }

    /**
     * 查询校区列表
     * 
     * @param campus 校区
     * @return 校区
     */
    @Override
    public List<Campus> selectCampusList(Campus campus)
    {
        return campusMapper.selectCampusList(campus);
    }

    /**
     * 新增校区
     * 
     * @param campus 校区
     * @return 结果
     */
    @Override
    public int insertCampus(Campus campus)
    {
        campus.setId(snowflakeIdWorker.nextId());
        campus.setCreateTime(DateUtils.getNowDate());
        return campusMapper.insertCampus(campus);
    }

    /**
     * 修改校区
     * 
     * @param campus 校区
     * @return 结果
     */
    @Override
    public int updateCampus(Campus campus)
    {
        campus.setUpdateTime(DateUtils.getNowDate());
        return campusMapper.updateCampus(campus);
    }

    /**
     * 批量删除校区
     * 
     * @param ids 需要删除的校区主键
     * @return 结果
     */
    @Override
    public int deleteCampusByIds(Long[] ids)
    {
        return campusMapper.deleteCampusByIds(ids);
    }

    /**
     * 删除校区信息
     * 
     * @param id 校区主键
     * @return 结果
     */
    @Override
    public int deleteCampusById(Long id)
    {
        return campusMapper.deleteCampusById(id);
    }

    /**
     * 批量导入
     *
     * @param campuses
     * @param updateSupport
     * @param operator
     * @return
     */
    @Override
    public String importCampus(List<Campus> campuses, boolean updateSupport, Long operator) {
        if (StringUtils.isNull(campuses) || campuses.size() == 0) {
            throw new ServiceException("导入校区数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Campus campus : campuses) {
            try {
                //查询校区名称是否重复
                Campus n = campusMapper.selectCampusByName(campus.getName());
                if (StringUtils.isNull(n)) {
                    campus.setId(snowflakeIdWorker.nextId());
                    campus.setCreateTime(DateUtils.getNowDate());
                    BeanValidators.validateWithException(validator, campus);
                    this.insertCampus(campus);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、名称 " + campus.getName() +  " 导入成功");
                } else if (updateSupport) {
                    campus.setId(n.getId());
                    campus.setUpdateTime(DateUtils.getNowDate());
                    BeanValidators.validateWithException(validator, campus);
                    this.updateCampus(campus);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、名称 " + campus.getName() +  " 更新成功");
                } else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、名称 " + campus.getName() +  " 已存在");

                }
                //返回异常数据
            } catch (Exception e) {
                //查询民族名称是否重复
                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + campus.getName() +  " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
